<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分布式 ID 生成 - Snowflake 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-container {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .code-dots {
            display: flex;
            gap: 8px;
        }
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
            color: #d4d4d4;
        }
        .keyword { color: #c586c0; }
        .type { color: #4ec9b0; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .number { color: #b5cea8; }
        .function { color: #dcdcaa; }
        .card {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        .card:hover {
            transform: translateY(-4px);
            box-shadow: 0 12px 24px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-size: 24px;
            margin-bottom: 16px;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
            margin: 3rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto animate-fadeInUp">
            <div class="text-center">
                <h1 class="text-5xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                    分布式 ID 生成算法
                </h1>
                <p class="text-2xl mb-4 opacity-90">Snowflake 雪花算法详解</p>
                <p class="text-lg opacity-80 max-w-3xl mx-auto">
                    在分布式系统中生成全局唯一、趋势递增的 ID 是一个经典问题。Snowflake 算法通过巧妙的位运算设计，实现了高性能、高可用的分布式 ID 生成方案。
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.1s;">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-6 text-gray-800" style="font-family: 'Noto Serif SC', serif;">
                    <i class="fas fa-puzzle-piece text-purple-600 mr-3"></i>题目描述
                </h2>
                <p class="text-lg text-gray-700 leading-relaxed">
                    设计一种分布式唯一ID生成算法，要求在高并发环境下生成的ID全局唯一且有序。这是分布式系统中的一个核心问题，常见的解决方案包括 Snowflake 算法、UUID、数据库自增等。
                </p>
            </div>
        </section>

        <!-- Core Concepts -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.2s;">
            <div class="grid md:grid-cols-2 gap-6">
                <div class="card bg-white rounded-2xl shadow-lg p-8">
                    <div class="feature-icon bg-gradient-to-br from-blue-500 to-purple-600 text-white">
                        <i class="fas fa-microchip"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">核心考点</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span>位运算的灵活运用</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span>分布式系统设计思想</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span>并发控制与同步机制</span>
                        </li>
                    </ul>
                </div>
                <div class="card bg-white rounded-2xl shadow-lg p-8">
                    <div class="feature-icon bg-gradient-to-br from-green-500 to-teal-600 text-white">
                        <i class="fas fa-lightbulb"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">算法特点</h3>
                    <ul class="space-y-3 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-star text-yellow-500 mt-1 mr-3"></i>
                            <span>时间复杂度 O(1)，性能极高</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-star text-yellow-500 mt-1 mr-3"></i>
                            <span>ID 趋势递增，对数据库索引友好</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-star text-yellow-500 mt-1 mr-3"></i>
                            <span>不依赖外部存储，可独立部署</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Algorithm Structure -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.3s;">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-6 text-gray-800" style="font-family: 'Noto Serif SC', serif;">
                    <i class="fas fa-sitemap text-purple-600 mr-3"></i>算法结构
                </h2>
                <p class="text-lg text-gray-700 mb-6">
                    Snowflake 算法使用 64 位整数来生成 ID，巧妙地将时间戳、机器标识和序列号组合在一起：
                </p>
                <div class="mermaid">
                    <div class="mermaid">
graph LR
    A[64位ID结构] --> B[1位<br/>符号位<br/>固定为0]
    A --> C[41位<br/>时间戳<br/>毫秒级]
    A --> D[10位<br/>机器ID<br/>最多1024台]
    A --> E[12位<br/>序列号<br/>每毫秒4096个]
    
    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
    style B fill:#ff6b6b,stroke:#fff,stroke-width:2px,color:#fff
    style C fill:#4ecdc4,stroke:#fff,stroke-width:2px,color:#fff
    style D fill:#45b7d1,stroke:#fff,stroke-width:2px,color:#fff
    style E fill:#96ceb4,stroke:#fff,stroke-width:2px,color:#fff
                    </div>
                </div>
                <div class="grid md:grid-cols-4 gap-4 mt-8">
                    <div class="text-center p-4 bg-gray-50 rounded-lg">
                        <div class="text-3xl font-bold text-red-500 mb-2">1 bit</div>
                        <div class="text-sm text-gray-600">符号位（恒为0）</div>
                    </div>
                    <div class="text-center p-4 bg-gray-50 rounded-lg">
                        <div class="text-3xl font-bold text-teal-500 mb-2">41 bits</div>
                        <div class="text-sm text-gray-600">时间戳（约69年）</div>
                    </div>
                    <div class="text-center p-4 bg-gray-50 rounded-lg">
                        <div class="text-3xl font-bold text-blue-500 mb-2">10 bits</div>
                        <div class="text-sm text-gray-600">机器ID（1024台）</div>
                    </div>
                    <div class="text-center p-4 bg-gray-50 rounded-lg">
                        <div class="text-3xl font-bold text-green-500 mb-2">12 bits</div>
                        <div class="text-sm text-gray-600">序列号（4096/ms）</div>
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- Solution Approach -->
        <section class="mb-12 animate-fadeInUp" style="animation-delay: 0.4s;">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-6 text-gray-800" style="font-family: 'Noto Serif SC', serif;">
                    <i class="fas fa-route text-purple-600 mr-3"></i>解题思路
                </h2>
                <div class="space-y-6">
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <span class="text-purple-600 font-bold">1</span>
                        </div>
                        <div>
                            <h4 class="text-xl font-semibold mb-2 text-gray-800">时间戳设计</h4>
                            <p class="text-gray-700">使用 41 位存储毫秒级时间戳，从自定义纪元开始计算，可以使用约 69 年。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <span class="text-purple-600 font-bold">2</span>
                        </div>
                        <div>
                            <h4 class="text-xl font-semibold mb-2 text-gray-800">机器标识</h4>
                            <p class="text-gray-700">10 位机器 ID 支持最多 1024 个节点，可以根据实际需求调整数据中心ID和机器ID的位数分配。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <span class="text-purple-600 font-bold">3</span>
                        </div>
                        <div>
                            <h4 class="text-xl font-semibold mb-2 text-gray-800">序列号机制</h4>
                            <p class="text-gray-700">12 位序列号支持同一毫秒内生成 4096 个 ID，通过同步机制保证并发安全。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <div class="flex-shrink-0 w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <span class="text-purple-600 font-bold">4</span>
                        